package com.dgut.doctorservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dgut.commonutils.Result;
import com.dgut.doctorservice.entity.Doctor;
import com.dgut.doctorservice.entity.PatientInfo;
import com.dgut.doctorservice.entity.Reception;
import com.dgut.doctorservice.entity.vo.ReceptionQuery;
import com.dgut.doctorservice.service.PatientInfoService;
import com.xiaoleilu.hutool.captcha.CaptchaUtil;
import com.xiaoleilu.hutool.captcha.LineCaptcha;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dgut
 * @since 2021-09-22
 */
@RestController
@RequestMapping("/doctorservice/patient-info")
@CrossOrigin
public class PatientInfoController {

    @Autowired
    PatientInfoService patientInfoService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    @ApiOperation("获取所有病人")
    @GetMapping("getAllPatient")
    public Result getAllPatient(){
        List<PatientInfo> list = patientInfoService.list();
        return Result.succ().data("list",list);
    }

    @ApiOperation("删除病人操作")
    @DeleteMapping("deletePatient")
    public Result deletePatient(@RequestParam String id){
        boolean b = patientInfoService.removeById(id);
        if (b){
            return Result.succ().message("删除成功！");
        }else {
            return Result.succ().message("删除失败！");
        }
    }

    @ApiOperation("对病人进行组合条件查询")
    @PostMapping("getPatients")
    public Result getPatients(@RequestBody ReceptionQuery receptionQuery){
        String name = receptionQuery.getName();
        String idCard = receptionQuery.getIdCard();
        String phone = receptionQuery.getPhone();
        QueryWrapper<PatientInfo> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(idCard)){
            wrapper.like("id_card",idCard);
        }
        if (!StringUtils.isEmpty(phone)){
            wrapper.like("phone",phone);
        }
        List<PatientInfo> list = patientInfoService.list(wrapper);
        return Result.succ().data("list",list);
    }


    @ApiOperation("修改病人登录密码")
    @PostMapping("updatePassword")
    public Result updatePassword(@RequestParam String id,@RequestParam String old,@RequestParam String update){
        PatientInfo byId = patientInfoService.getById(id);
        if (byId.getPassword().equals(old)){
            byId.setPassword(update);
            patientInfoService.updateById(byId);
            return Result.succ();
        }else {
            return Result.succ().message("原密码错误！");
        }
    }


    @ApiOperation("获取随机验证码图片")
    @GetMapping("getImage")
    public Result getImage(HttpServletResponse response){
        LineCaptcha captcha= CaptchaUtil.createLineCaptcha(200,100,4,2);
        String img=captcha.getImageBase64();
        String code=captcha.getCode();
        System.out.println(code);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("captcha","data:image/png;base64,"+img);
        map.put("verify", UUID.randomUUID().toString().replace("-","")+new Date().getTime());
        return Result.succ().data("map",map).data("code",code);
    }


//    @ApiOperation("病人进行注册")
//    @PostMapping("patientRegister")
//    public Result patientRegister(@RequestBody PatientInfo patientInfo){
//        patientInfoService.save(patientInfo);
//        QueryWrapper<PatientInfo> wrapper = new QueryWrapper<>();
//        wrapper.eq("phone",patientInfo.getPhone());
//        PatientInfo one = patientInfoService.getOne(wrapper);
//        one.setPassword(null);
//        return Result.succ().data("user",one);
//    }
    @ApiOperation("病人进行手机验证码注册")
    @PostMapping("patientRegister")
    public Result patientRegister(@RequestBody PatientInfo patientInfo,String code){
        QueryWrapper<PatientInfo> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("phone",patientInfo.getPhone());
        PatientInfo one1 = patientInfoService.getOne(wrapper1);
        if (one1!=null){
            return Result.succ().message("该手机号码已经注册!");
        }else{
            patientInfoService.save(patientInfo);
            QueryWrapper<PatientInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("phone",patientInfo.getPhone());
//     从redis中获取验证码方法 code为前端页面输入的验证码
//      判断手机验证码和输入的验证码是否一致

            String redisCode = redisTemplate.opsForValue().get(patientInfo.getPhone());
            if(!code.equals(redisCode)) {
                return Result.fail().message("手机验证码错误");
            }

            PatientInfo one = patientInfoService.getOne(wrapper);
            one.setPassword(null);
            return Result.succ().data("user",one);
        }
    }


    @ApiOperation("病人进行登录")
    @PostMapping("patientLogin")
    public Result patientLogin(@RequestParam String phone,@RequestParam String password){
        QueryWrapper<PatientInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",phone);
        PatientInfo one = patientInfoService.getOne(wrapper);
        if (one==null){
            return Result.fail().message("未存在账户");
        }else {
            if (one.getPassword().equals(password)){
                one.setPassword(null);
                return Result.succ().data("patient",one);
            }else {
                return Result.fail().message("密码错误");
            }
        }
    }

    @ApiOperation("根据用户的id进行查找返回，可用于用户信息进行回显修改")
    //根据id进行查询
    @GetMapping("GetPatientById/{id}")
    public Result getPatientById(@PathVariable String id){
        PatientInfo patientInfo = patientInfoService.getById(id);

        patientInfo.setPassword(null);
        return Result.succ().data("patient",patientInfo);
    }

    @ApiOperation("修改用户信息")
    @PostMapping("updatePatient")
    public Result updatePatient(@RequestBody PatientInfo patient){
        boolean flag = patientInfoService.updateById(patient);
        if (flag){
            return Result.succ();
        }else {
            return Result.fail();
        }

    }
}

